package com.cpi.cornpp.customerpurchase.converter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.cpi.cornpp.customerpurchase.dto.CustomerPurchaseDTO;
import com.cpi.cornpp.customerpurchase.view.CustomerPurchaseView;
import com.cs.common.init.SystemParameterCollection;
import com.cs.common.init.beans.SystemParameterBean;

public class CustomerPurchaseConverter {
/**
	private int cornFutureDistributionId;
	private int cornFutureId;
	private String customerId;
	private String futureTypeId;
	private int quantity;
 * @param customerPurchaseDTO
 * @return
 */
	
	/** Codigo de parametro del future type fixed*/
	private static String FUTURE_TYPE_FIXED = "FUTURE_TYPE_FIXED";
	
	/** Codigo de parametro del future type direct*/
	private static String FUTURE_TYPE_DIRECT = "FUTURE_TYPE_DIRECT";

	/** Codigo del future type direct*/
	private static String futureTypeFixed = null;
	
	/** Codigo del future type direct*/
	private static String futureTypeDirect = null;
	
	static{
		SystemParameterCollection systemParameterCollection = null;
		SystemParameterBean systemParameterBean = null;	
		
		systemParameterCollection = SystemParameterCollection.getInstance();
		systemParameterBean = (SystemParameterBean)systemParameterCollection.get(FUTURE_TYPE_FIXED);
		futureTypeFixed = systemParameterBean.getValue();
		
		systemParameterBean = (SystemParameterBean)systemParameterCollection.get(FUTURE_TYPE_DIRECT);
		futureTypeDirect = systemParameterBean.getValue();
	}
	public static final CustomerPurchaseView convert(CustomerPurchaseDTO cpDTO){
		CustomerPurchaseView cpView = null;


		
		if (cpDTO != null){
			cpView = new CustomerPurchaseView();			
			cpView.setFutureDistributionId(cpDTO.getCornFutureDistributionId());			
			cpView.setFutureId(cpDTO.getCornFutureId());
			cpView.setCustomerId(cpDTO.getCustomerId());
			cpView.setDescription(cpDTO.getDescription());
			cpView.setFutureTypeId(cpDTO.getFutureTypeId());
			if(cpDTO.getFutureTypeId().trim().equals(futureTypeFixed)){
				cpView.setQuantityFixed(cpDTO.getQuantity());
			}else{
				cpView.setQuantityDirect(cpDTO.getQuantity());
			}
			cpView.setQuantity(cpDTO.getQuantity());
		}
		
		return cpView;
	}
	
	public static final CustomerPurchaseDTO convert(CustomerPurchaseView cpView){
		CustomerPurchaseDTO cpDTO = null;
		
		if (cpView != null){
			cpDTO = new CustomerPurchaseDTO();						
			cpDTO.setCornFutureDistributionId(cpView.getFutureDistributionId());			
			cpDTO.setCornFutureId(cpView.getFutureId());
			cpDTO.setCustomerId(cpView.getCustomerId());			
			if(cpView.getQuantityFixed()!=0){
				cpDTO.setQuantity(cpView.getQuantityFixed());
				cpDTO.setFutureTypeId(futureTypeFixed);
			}
			else{
				cpDTO.setQuantity(cpView.getQuantityDirect());
				cpDTO.setFutureTypeId(futureTypeDirect);
			}
			cpDTO.setDescription(cpView.getDescription());


		}
		
		return cpDTO;
	}
	
	public static final List convertViews(List customerViews){
		List customers = null;
		customers = new ArrayList(0);
		CustomerPurchaseView view = null;
		Iterator iterator = customerViews.iterator();
		if (!CollectionUtils.isEmpty(customerViews)){
			while( iterator.hasNext() ){
				view = (CustomerPurchaseView)iterator.next();
				customers.add(convert(view));
			}
		}		
		return customers;
	}
	
	public static final List convertDTOs(Collection customerDTO){
		List customers = null;
		customers = new ArrayList(0);
		CustomerPurchaseDTO dto = null;
		Iterator iterator = customerDTO.iterator();
		if (!CollectionUtils.isEmpty(customerDTO)){
			while( iterator.hasNext()){
				dto = (CustomerPurchaseDTO)iterator.next();
				customers.add(convert(dto));
			}
		}		
		return customers;
	}	
}
